<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>menu</title>
    <link media="all" rel="stylesheet" th:href="@{/libs/layui-v2.5.5/css/layui.css}">
    <link media="all" rel="stylesheet" th:href="@{/libs/font-awesome-4.7.0/css/font-awesome.min.css}">
    <link media="all" rel="stylesheet" th:href="@{/css/public.css}">
    <style>
        .layui-btn:not(.layui-btn-lg ):not(.layui-btn-sm):not(.layui-btn-xs) {
            height: 34px;
            line-height: 34px;
            padding: 0 8px;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <div>
            <div class="layui-btn-group">
                <button class="layui-btn" id="btn-expand">全部展开</button>
                <button class="layui-btn layui-btn-normal" id="btn-fold">全部折叠</button>
                <button class="layui-btn layui-btn-warm" id="add-menu">添加菜单</button>
            </div>
            <table class="layui-table" id="munu-table" lay-filter="munu-table"></table>
        </div>
    </div>
</div>

<!-- 操作列 -->
<script id="auth-state" type="text/html">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script charset="utf-8" th:src="@{/libs/layui-v2.5.5/layui.js}"></script>
<script charset="utf-8" th:src="@{/js/lay-config.js}"></script>
<script>
    layui.use(['table', 'treetable'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var treetable = layui.treetable;

        // 渲染表格
        layer.load(2);
        treetable.render({
            treeColIndex: 1,
            treeSpid: -1,
            treeIdName: 'id',
            treePidName: 'parentId',
            elem: '#munu-table',
            url: '/system/menu/list',
            page: false,
            cols:
                [
                    [
                        {type: 'numbers'},
                        {field: 'title', minWidth: 200, title: '菜单名'},
                        {field: 'url', align: 'center', title: '菜单url'},
                        {
                            field: 'icon', title: '图标', align: 'center', templet: function (d) {
                                return '<i class="fa ' + d.icon + '" style="width: 30px"></i>'
                            }
                        },
                        {field: 'idx', align: 'center', title: '排序号'},
                        {
                            field: 'isMenu', align: 'center', templet: function (d) {
                                // if (d.isMenu == 1) {
                                //     return '<span class="layui-badge layui-bg-gray">按钮</span>';
                                // }
                                if (d.parentId === -1) {
                                    return '<span class="layui-badge layui-bg-blue">目录</span>';
                                } else {
                                    return '<span class="layui-badge-rim">菜单</span>';
                                }
                            }, title: '类型'
                        },
                        {field: 'remark', title: '备注'},
                        {templet: '#auth-state', width: 120, align: 'center', title: '操作'}
                    ]
                ],
            done: function () {
                layer.closeAll('loading');
            }
        });

        $('#btn-expand').click(function () {
            treetable.expandAll('#munu-table');
        });

        $('#btn-fold').click(function () {
            treetable.foldAll('#munu-table');
        });

        $('#add-menu').click(function () {
            var index = layer.open({
                title: '添加菜单',
                type: 2,
                shade: 0.2,
                maxmin: true,
                shadeClose: true,
                area: ['100%', '100%'],
                content: '/system/menu/add',
                end: function () {
                    location.reload();
                }
            });
            $(window).on("resize", function () {
                layer.full(index);
            });
        });


        //监听工具条
        table.on('tool(munu-table)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;
            if (layEvent === 'del') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        type: "DELETE",
                        url: "/system/menu/delete/" + data.id,
                        dataType: "json",
                        success: function (data) {
                            layer.alert(data.message, function () {
                                location.reload();
                            });
                        }
                    });
                });
            } else if (layEvent === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin: true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: '/system/menu/edit/' + data.id,
                    end: function () {
                        location.reload();
                    }
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });
    });
</script>
</body>
</html>